import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast } 	from 'vant';
Vue.use(Toast);

export default {
	name: 'orderDetail',
	data(){
		return {
			payType:0,			//支付方式  1:支付宝  2:银行卡  3:微信
			interface:false,	//接口是否已返回
			id:'',				//订单ID
			pageState:0,		//0:待定  1:购买订单   2:出售订单
			orderStatus:0,		//参照 orderList页面 state状态
			logo:'',			//图标
			name:'',			//币种名称
			createTime:'',		//下单时间
			orderrmb:'',		//转账金额
			orderusdt:'',		//数量
			imgUrl:'',			//图片状态路径
			desc:'',			//状态描述
			desc2:'',			//说明描述
			haveMessage:false,	//是否有商家消息
			dropName:''
		}
	},
	methods:{
		getRed(){
			//获取小红点数量
			let self = this;
			if(!self.dropName) return
			util.callHandler('getSupplierUnreadMsgCount',{
				supplierName:self.dropName
			},res => {
				self.haveMessage = res.count > 0?true:false;
			})
			setTimeout(()=>{
				self.getRed();
			},3000)
		},
		init(){
			let _url = util.ajaxUrl + (this.pageState == 1?'/otc/inmoney-detail':'/otc/outmoney-detail');
			util.fetch(_url,{
				id:this.id
			}).then(res => {
				Toast.clear();
				this.interface = true;
				if(res.retCode === 0){
					this.name = res.retData.currency_id;
					//从本地匹配可能存在的logo
					let otcConfig = util.getStorage('otcConfig');
					if(otcConfig){
						otcConfig = JSON.parse(otcConfig);
						if(otcConfig.currency[this.name]){
							this.logo = otcConfig.currency[this.name].logo
						}
					}
					if(this.pageState==1) this.payType  = (res.retData.outtype==3 || res.retData.outtype==4)?3:res.retData.outtype;				//支付方式
					if(this.pageState==2) this.payType  = (res.retData.intype==3 || res.retData.intype==4)?3:res.retData.intype;				//支付方式
					this.createTime = util.transTime(res.retData.create_time,1);
					this.orderrmb = res.retData.orderrmb;
					this.orderusdt = res.retData.orderusdt;
					this.orderStatus = res.retData.state;
					this.dropName = res.retData.outname;				//收款账户
					this.calcPageShow();
					setTimeout(() => {
						if(this.orderStatus != '-99') this.init();
					},5000)
				}else{
					Toast(res.retMsg || '未知错误')
				}
			}).catch(error => {
				Toast.clear();
				this.interface = true;
				console.log(error)
			})
		},
		calcPageShow(){
			//计算图片以及文字显示内容
			let _dot = process.env.NODE_ENV === 'development'?'../../':'./';
			if(this.pageState == 1){
				//购买订单
				switch(this.orderStatus){
					case('1'):
						this.imgUrl = _dot+'static/images/order-detail-wait.png';
						this.desc = '请稍等，订单处理中';
						this.desc2 = '·您的订单已收到。正在为您入账。如在交易高峰期，系统需要5-10分钟来处理订单，请您耐心等待';
						break;
					case('2'):
						this.imgUrl = _dot+'static/images/order-detail-wait.png';
						this.desc = '请稍等，订单处理中';
						this.desc2 = '·您的订单已收到。正在为您入账。如在交易高峰期，系统需要5-10分钟来处理订单，请您耐心等待';
						break;
					case('-99'):
						this.imgUrl = _dot+'static/images/order-detail-cancel.png';
						this.desc = '订单已取消';
						break;
					case('99'):
						this.imgUrl = _dot+'static/images/order-detail-cancel.png';
						this.desc = '订单已取消';
						break;
					case('3'):
						this.imgUrl = _dot+'static/images/order-detail-done.png';
						this.desc = '订单已完成';
						break;
					case('4'):
						this.imgUrl = _dot+'static/images/order-detail-wait.png';
						this.desc = '请稍等，订单处理中';
						this.desc2 = '·您的订单已收到。正在为您入账。如在交易高峰期，系统需要5-10分钟来处理订单，请您耐心等待';
						break;
					default:
						this.imgUrl = '';
						this.desc = '未知状态';
				}
			}else if(this.pageState == 2){
				// console.log(this.orderStatus)
				switch(this.orderStatus){
					case('99'):
						this.imgUrl = _dot+'static/images/order-detail-done.png';
						this.desc = '订单已完成';
						this.desc2 = '·资金到账大约需要1~3分钟';
						break;
					case('-1'):
						this.imgUrl = _dot+'static/images/order-detail-done.png';
						this.desc = '订单已完成';
						this.desc2 = '·资金到账大约需要1~3分钟';
						break;
					case('-2'):
						this.imgUrl = _dot+'static/images/order-detail-wait.png';
						this.desc = '请稍等，订单处理中';
						this.desc2 = '·您的订单已收到。正在为您入账。如在交易高峰期，系统需要5-10分钟来处理订单，请您耐心等待';
						break;
					case('-3'):
						this.imgUrl = _dot+'static/images/order-detail-reject.png';
						this.desc = '订单已取消';
						break;
					case('-4'):
						this.imgUrl = _dot+'static/images/order-detail-wait.png';
						this.desc = '请稍等，订单处理中';
						this.desc2 = '·您的订单已收到。正在为您入账。如在交易高峰期，系统需要5-10分钟来处理订单，请您耐心等待';
						break;
					case('-5'):
						this.imgUrl = _dot+'static/images/order-detail-wait.png';
						this.desc = '待转币';
						this.desc2 = '·您的订单已收到。正在为您入账。如在交易高峰期，系统需要5-10分钟来处理订单，请您耐心等待';
						break;
					case('-99'):
						this.imgUrl = _dot+'static/images/order-detail-cancel.png';
						this.desc = '订单已取消';
						break;
					default:
						this.imgUrl = '';
						this.desc = '未知状态'
				}
			}
		},
		goBack(){
			util.navigateBack(-1,this)
		},
		goContact(){
			if(this.version < 410){
				this.updateDialog();
				// Toast('请升级至最新版本后使用该功能'+this.version)
				return
			}
			// console.log('bbjApp://contractSupplier?orderId='+ this.id +'&supplierName='+this.dropName+'&orderType='+Number(this.pageState))
			if(this.pageState == 2) {
				this.dropName = '商家';
			}
			util.callHandler('openUrl',{
				url:'bbjApp://contractSupplier?orderId='+ this.id +'&supplierName='+this.dropName+'&orderType='+Number(this.pageState)+'&payType='+this.payType,
				title:'联系商家',
				needLogin:true,
				popVC:false,		//跳转的时候是否干掉当前页面
				hiddenNav:true,		//是否全屏
			},res => {
				console.log(res)
			})
		},
		service(){
			util.callHandler('openUrl',{
				url:'bbjApp://servicerinfo',
				title:'联系客服',
				needLogin:true,
				popVC:false,		//跳转的时候是否干掉当前页面
				hiddenNav:true,		//是否全屏
			},res => {
				console.log(res)
			})
		},
	},
	created() {
		Toast.loading({
			message: '加载中...',
			duration:0,
			forbidClick: true
		});
		this.getRed();
		util.waitSDKReady().then(token => {
			this.pageState = this.$route.query.pageState;
			sessionStorage.setItem('pageState',this.pageState)
			this.id = this.$route.query.id;
			util.setStorage('orderId',this.id);
			// return
			this.init();
		}).catch((e) => {
			Toast('获取token失败')
		})
		setTimeout(() => {
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		},500)
    }
}